//
//  PageViewController.m
//  CorssScroll
//
//  Created by nsm on 2019/2/21.
//  Copyright © 2019 nani. All rights reserved.
//

#import "PageViewController.h"

static const CGFloat kSuspendHeight = 48;

@interface PageViewController () <NSMPageControllerDelegate, JXCategoryViewDelegate>
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) NSArray *pageViews;
@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@end

@implementation PageViewController
    
#pragma mark - lifeCycle

#pragma mark - override

#pragma mark - initialization
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    
    NSMutableArray *pageViews = @[].mutableCopy;
    NSArray *titles = @[@"条目1", @"条目2", @"条目3"];
    for (int i=0; i<titles.count; i++) {
        NSMPageView *pageView = NSMPageView.new;
        pageView.viewDidAppear = ^(NSMPageView *sender) {
            NSLog(@"我要显示了:%@", titles[i]);
        };
        pageView.backgroundColor = RandomColor();
        [pageViews addObject:pageView];
    }
    
    self.pageViews = pageViews;
    //self.tableView.bounces = NO;
    self.delegate = self;
    self.headerImageView.image = [UIImage imageNamed:@"header_bg"];
    self.headerImageView.backgroundColor = RandomColor();
    [self reloadData];
    
    self.categoryView.delegate = self;
    self.categoryView.titles = titles;
    self.categoryView.titleFont = [UIFont systemFontOfSize:15];
    self.categoryView.titleSelectedFont = [UIFont systemFontOfSize:15];
    self.categoryView.titleColor = UIColor.grayColor;
    self.categoryView.titleSelectedColor = UIColor.blackColor;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.contentScrollView = self.containerView;
    
    JXCategoryIndicatorLineView *line = [[JXCategoryIndicatorLineView alloc] init];
    line.indicatorLineWidth = 27;
    line.indicatorLineViewColor = UIColor.blackColor;
    line.lineStyle = JXCategoryIndicatorLineStyle_IQIYI;
    self.categoryView.indicators = @[line];
    /*
    {
        NSMSMSButton *smsBtn = NSMSMSButton.new;
        smsBtn.backgroundColor = UIColor.blackColor;
        [smsBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        smsBtn.size = CGSizeMake(100, 30);
        smsBtn.countdownPolicy = NSMSMSCountdownDoneElseRestart;
        smsBtn.groupTag = 1;
        smsBtn.center = CGPointMake(kScreenWidth/2, kScreenHeight/2);
        smsBtn.onActionHandler = ^(void (^completion)(BOOL)) {
            completion(YES);
        };
        [self.view addSubview:smsBtn];
        [smsBtn reloadAndUpdate];
    }
    {
        NSMSMSButton *smsBtn = NSMSMSButton.new;
        smsBtn.backgroundColor = UIColor.blackColor;
        [smsBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        smsBtn.size = CGSizeMake(100, 30);
        smsBtn.countdownPolicy = NSMSMSCountdownUntilDone ;
        smsBtn.center = CGPointMake(kScreenWidth/2, kScreenHeight/2+100);
        smsBtn.onActionHandler = ^(void (^completion)(BOOL)) {
            completion(YES);
        };
        [self.view addSubview:smsBtn];
        [smsBtn reloadAndUpdate];
    }
     */
}

#pragma mark - NSMPageControllerDelegate
- (CGFloat)heightForSuspendInPageController:(NSMPageController *)pageController {
    return kSuspendHeight;
}

- (NSUInteger)numberOfViewsInPageController:(NSMPageController *)pageController {
    return self.pageViews.count;
}

- (UIView *)viewForHeaderInPageController:(NSMPageController *)pageController {
    return self.headerView;
}

- (UIView *)viewForSuspendInPageController:(NSMPageController *)pageController {
    return self.categoryView;
}

- (NSMPageView *)pageController:(NSMPageController *)pageController viewForIndex:(NSUInteger)index {
    return self.pageViews[index];
}

#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
}

#pragma mark - setter/getter
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = UIView.new;
        _headerView.width = UIScreen.mainScreen.bounds.size.width;
        
        UILabel *label = UILabel.new;
        label.origin = CGPointMake(15, 15);
        label.width = _headerView.width - 15*2;
        label.text = @"鹅鹅鹅...\n曲项向天歌,\n白毛浮绿水,\n红掌拨清波.";
        label.numberOfLines = 0;
        label.textColor = UIColor.yellowColor;
        [_headerView addSubview:label];
        
        [label sizeToFit];
        
        _headerView.height = label.bottom+15;
    }
    return _headerView;
}

- (JXCategoryTitleView *)categoryView {
    if (!_categoryView) {
        CGRect frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, kSuspendHeight);
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:frame];
        _categoryView.backgroundColor = UIColor.whiteColor;
        
        _categoryView.layer.shadowColor = UIColor.redColor.CGColor;
        _categoryView.layer.shadowOffset = CGSizeMake(0, 2);
        _categoryView.layer.shadowOpacity = 0.7;
        _categoryView.layer.shadowRadius = 3;
    }
    return _categoryView;
}

#pragma mark - network

#pragma mark - method/public

#pragma mark - method/private

#pragma mark - action

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
